package com.che300.test.demos.web;

import com.che300.test.demos.web.common.HttpServerCodeEnum;
import com.che300.test.demos.web.common.ThirdPartyApiClient;
import com.che300.test.dto.QueryCompanyResponse;
import com.che300.test.demos.web.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @author chenjl26787
 * @version 1.0
 * @date 2025-02-23$ 20:05
 */

@RestController
@RequestMapping("/api/phone")
@CrossOrigin(origins = "*") // 允许跨域
public class PhoneController {
    @Autowired
    private ThirdPartyApiClient thirdPartyApiClient;
    // 查询归属地
    @GetMapping("/location")
    @Cacheable(value = "phoneLocationCache", key = "#phone")
    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 1000,multiplier = 1.5))
    public Result getLocation(@RequestParam String phone) {
        if(phone == null ||phone.length()==0) return new Result(HttpServerCodeEnum.PHONE_ERROR);
        if(phone.matches("1\\d{2}")){
            // 随机生成剩余的 8 位数字
            String randomSuffix = generateRandomPhoneNumberSuffix();
            String fullPhoneNumber = phone + randomSuffix;
            QueryCompanyResponse queryProvinceResponse = new QueryCompanyResponse();
            queryProvinceResponse.setCompany(thirdPartyApiClient.queryMobileLocation(fullPhoneNumber).getCompany());
            return new Result(HttpServerCodeEnum.SUCCESS,queryProvinceResponse);
        } else if (phone.matches("1\\d{10}")) {
            if (phone.length() != 11 || !phone.matches("1\\d{10}")) {
                return new Result(HttpServerCodeEnum.PHONE_ERROR);
            }
            //查询前三位
            return  new Result(HttpServerCodeEnum.SUCCESS,thirdPartyApiClient.queryMobileLocation(phone));
        }
        else return new Result(HttpServerCodeEnum.PHONE_ERROR);

    }

//    @GetMapping("/company")
//    public Result getOperator(@RequestParam String prefix) {
//        if (prefix == null || prefix.length() != 3 || !prefix.matches("1\\d{2}")) {
//            return Result.error("请输入有效的手机号前三位");
//        }
//
//        // 随机生成剩余的 8 位数字
//        String randomSuffix = generateRandomPhoneNumberSuffix();
//        String fullPhoneNumber = prefix + randomSuffix;
//        QueryCompanyResponse queryProvinceResponse = new QueryCompanyResponse();
//        queryProvinceResponse.setCompany(thirdPartyApiClient.queryMobileLocation(fullPhoneNumber).getCompany());
//        return Result.success(queryProvinceResponse);
//    }
    // 随机生成 8 位数字
    private String generateRandomPhoneNumberSuffix() {
        Random random = new Random();
        StringBuilder suffix = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            suffix.append(random.nextInt(10)); // 随机生成 0-9 的数字
        }
        return suffix.toString();
    }
}

